import { createAction } from 'redux-actions'
import { check, body, delJson, getJson, postJson, putJson, parseJSON, checkStatus, data } from './util'

// 新员工获取profile
export
const fetchOnboardingProfile = createAction('fetch onboarding profile', () =>
  fetch(`/onboarding-flows/staff/complete-profile`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(data)
)

// 新员工获取core-profile
export
const fetchOnboardingCoreProfile = createAction('fetch onboarding core profile', () =>
	fetch('/onboarding-flows/staff/core-profile', getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(data)
)

export
const updateOnboardingProfile = createAction('update onboarding profile')

export
const saveOnboardingProfile = createAction('save onboarding profile', (arg, cb) =>
	fetch('/onboarding-flows/staff/user-profile/fields', {
		...putJson,
		...body(arg)
	})
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(res => {
			cb && cb()
			return res.data
		})
)

// 新入职员工获取自己的模板
export
const fetchOnboardingStaffTemplate = createAction('fetch onboarding template', () => 
  fetch('/onboarding-flows/staff/onboarding-template', getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(data)
)

export
const resetOnboardingStaff = createAction('reset onboarding staff')

export
const downloadTempDocument = createAction('_', (documentId, cb) =>
  fetch(`/documents/temp/${documentId}/download`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(cb)
)

export
const downloadStaffDocument = createAction('_', (documentId, cb) =>
  fetch(`/onboarding-flows/staff/onboarding-documents/${documentId}/download`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(cb)
)

export
const submitStaffSubmission = createAction('_', cb =>
  fetch('/onboarding-flows/staff/submission', putJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(cb)
)

export
const createStaffDocument = createAction('create staff document', (arg, cb) =>
  fetch('/documents/temp', {
    ...postJson,
    ...body(arg)
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(res => {
			cb && cb(res.data)
			return res.data
		})
)
